﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.ExchangeAudit
{
    public partial class ExchangeAuditEdit_ZY : System.Web.UI.Page
    {
        ExchangeAuditDal dal = new ExchangeAuditDal();

        private string FlowInstanceID
        {
            get
            {
                if (ViewState["FlowInstanceID"] != null)
                {
                    return ViewState["FlowInstanceID"].ToString();
                }
                else
                {
                    string flowInstanceID = Guid.NewGuid().ToString();
                    ViewState["FlowInstanceID"] = flowInstanceID;
                    return flowInstanceID;
                }
            }
            set
            {
                ViewState["FlowInstanceID"] = value;
            }
        }

        private string Source
        {
            get
            {
                object obj = ViewState["Source"];
                return obj != null ? obj.ToString() : string.Empty;
            }
            set
            {
                ViewState["Source"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FlowInstanceID = Request.QueryString["FlowInstanceID"];
                this.Source = Request.QueryString["Source"];

                InitControlData();
                SetControlState();
            }
        }

        private void InitControlData()
        {
            string sql = @"SELECT * FROM t5715_ExchangeAudit_ZY WHERE FlowInstanceID = @FlowInstanceID";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, this.FlowInstanceID);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                DataRow row = dataTable.Rows[0];
                lblCode.Text = row["Code"].ToString();
                txtSourceDispatchCode.Text = row["SourceDispatchCode"].ToString();
                hidSourceDispatchCode.Value = row["SourceDispatchCode"].ToString();

                txtSourceMissileCode.Text = row["SourceMissileCode"].ToString();
                hidSourceMissileCode.Value = row["SourceMissileCode"].ToString();

                txtSourceProcess.Text = row["SourceProcess"].ToString();
                hidSourceProcess.Value = row["SourceProcess"].ToString();

                txtSourcePartCode.Text = row["SourcePartCode"].ToString();
                txtSourcePartNumber.Text = row["SourcePartNumber"].ToString();
                
                txtTargeDispatchCode.Text = row["TargeDispatchCode"].ToString();
                hidTargeDispatchCode.Value = row["TargeDispatchCode"].ToString();

                txtTargeMissileCode.Text = row["TargeMissileCode"].ToString();
                hidTargeMissileCode.Value = row["TargeMissileCode"].ToString();

                txtTargeProcess.Text = row["TargeProcess"].ToString();
                hidTargeProcess.Value = row["TargeProcess"].ToString();

                txtTargePartCode.Text = row["TargePartCode"].ToString();
                txtTargePartNumber.Text = row["TargePartNumber"].ToString();
            }
            else
            {
                lblCode.Text = dal.CreateApplyCode();
            }
        }

        private void SetControlState()
        {
            bool hasFlowData = false;
            bool flowCompleted = false;
            bool currentFlowStepIsFirstSetp = false;
            string flowEndOperation = string.Empty;

            if (!this.Source.ToLower().Equals("list"))
            {
                txtSourceDispatchCode.Enabled = false;
                txtSourceMissileCode.Enabled = false;
                txtSourceProcess.Enabled = false;
                txtSourcePartCode.Enabled = false;
                txtSourcePartNumber.Enabled = false;
                txtTargeDispatchCode.Enabled = false;
                txtTargeMissileCode.Enabled = false;
                txtTargeProcess.Enabled = false;
                txtTargePartCode.Enabled = false;
                txtTargePartNumber.Enabled = false;
                btnOK.Visible = false;
                btnCancel.Visible = false;

                return;
            }

            DataTable flowInstanceData = FlowHelper.GetFlowInstanceInfo(new Guid(this.FlowInstanceID));
            if (flowInstanceData != null && flowInstanceData.Rows.Count > 0)
            {
                currentFlowStepIsFirstSetp = FlowHelper.CurrentFlowStepIsFirstSetp(flowInstanceData.Rows[0]["FlowID"].ToString()
                    , this.FlowInstanceID);

                hasFlowData = true;

                if (flowInstanceData.Rows[0]["Completed"] != null)
                {
                    string strCompleted = flowInstanceData.Rows[0]["Completed"].ToString();

                    if (bool.TryParse(strCompleted, out flowCompleted) && flowCompleted)
                    {
                        if (flowInstanceData.Rows[0]["OperationID"] != null)
                        {
                            flowEndOperation = flowInstanceData.Rows[0]["OperationID"].ToString();
                        }
                    }
                }
            }

            if ((hasFlowData && !currentFlowStepIsFirstSetp))
            {
                txtSourceDispatchCode.Enabled = false;
                txtSourceMissileCode.Enabled = false;
                txtSourceProcess.Enabled = false;
                txtSourcePartCode.Enabled = false;
                txtSourcePartNumber.Enabled = false;
                txtTargeDispatchCode.Enabled = false;
                txtTargeMissileCode.Enabled = false;
                txtTargeProcess.Enabled = false;
                txtTargePartCode.Enabled = false;
                txtTargePartNumber.Enabled = false;
                btnOK.Visible = false;
            }            
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateData();
                Response.Redirect("ExchangeAuditList_ZY.aspx");
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败');</script>"); 
            }
        }

        private void UpdateData()
        {
            string sql_select = @"SELECT COUNT(FlowInstanceID) FROM dbo.t5715_ExchangeAudit_ZY WHERE FlowInstanceID = @FlowInstanceID";

            string sql_insert = @"INSERT INTO dbo.t5715_ExchangeAudit_ZY
                                       ([FlowInstanceID]
                                       ,[Code]
                                       ,[SourceDispatchCode]
                                       ,[SourceMissileCode]
                                       ,[SourceProcess]
                                       ,[SourcePartCode]
                                       ,[SourcePartNumber]
                                       ,[TargeDispatchCode]
                                       ,[TargeMissileCode]
                                       ,[TargeProcess]
                                       ,[TargePartCode]
                                       ,[TargePartNumber]
                                       ,[CreateUser]
                                       ,[CreateDate])
                                 VALUES
                                       (@FlowInstanceID
                                       ,@Code
                                       ,@SourceDispatchCode
                                       ,@SourceMissileCode
                                       ,@SourceProcess
                                       ,@SourcePartCode
                                       ,@SourcePartNumber
                                       ,@TargeDispatchCode
                                       ,@TargeMissileCode
                                       ,@TargeProcess
                                       ,@TargePartCode
                                       ,@TargePartNumber
                                       ,@CreateUser
                                       ,GETDATE())";
            string sql_update = @"UPDATE dbo.t5715_ExchangeAudit_ZY
                                       SET Code = @Code
                                          ,SourceDispatchCode = @SourceDispatchCode
                                          ,SourceMissileCode = @SourceMissileCode
                                          ,SourceProcess = @SourceProcess
                                          ,SourcePartCode = @SourcePartCode
                                          ,SourcePartNumber = @SourcePartNumber
                                          ,TargeDispatchCode = @TargeDispatchCode
                                          ,TargeMissileCode = @TargeMissileCode
                                          ,TargeProcess = @TargeProcess
                                          ,TargePartCode = @TargePartCode
                                          ,TargePartNumber = @TargePartNumber
                                          ,CreateUser = @CreateUser
                                     WHERE FlowInstanceID = @FlowInstanceID";

            try
            {

                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql_select);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, this.FlowInstanceID );
                string count = database.ExecuteScalar(dbCommand).ToString();

                if (count.Equals("0"))
                {
                    dbCommand = database.GetSqlStringCommand(sql_insert);
                }
                else
                {
                    dbCommand = database.GetSqlStringCommand(sql_update);
                }

                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, this.FlowInstanceID);
                database.AddInParameter(dbCommand, "@Code", DbType.String, lblCode.Text);
                database.AddInParameter(dbCommand, "@SourceDispatchCode", DbType.String, hidSourceDispatchCode.Value);
                database.AddInParameter(dbCommand, "@SourceMissileCode", DbType.String, hidSourceMissileCode.Value);
                database.AddInParameter(dbCommand, "@SourceProcess", DbType.String, hidSourceProcess.Value);
                database.AddInParameter(dbCommand, "@SourcePartCode", DbType.String, txtSourcePartCode.Text);
                database.AddInParameter(dbCommand, "@SourcePartNumber", DbType.String, txtSourcePartNumber.Text);
                database.AddInParameter(dbCommand, "@TargeDispatchCode", DbType.String, hidTargeDispatchCode.Value);
                database.AddInParameter(dbCommand, "@TargeMissileCode", DbType.String, hidTargeMissileCode.Value);
                database.AddInParameter(dbCommand, "@TargeProcess", DbType.String, hidTargeProcess.Value);
                database.AddInParameter(dbCommand, "@TargePartCode", DbType.String, txtTargePartCode.Text);
                database.AddInParameter(dbCommand, "@TargePartNumber", DbType.String, txtTargePartNumber.Text);
                database.AddInParameter(dbCommand, "@CreateUser", DbType.String, CurrentInfo.Instance.CurrentUser.UserName);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ExchangeAuditList_ZY.aspx");
        }
    }
}